package com.imooc.rabbti.comsumer.component;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @description: rabbitmq的监听器
 * @author: Yz
 * @create: 2020-09-25 15:37
 **/
@Component
public class RabbitReceive {

    /**
     * 组合使用监听
     * @param message
     * @param channel
     * @throws Exception
     */
    @RabbitHandler
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "queue-1",durable = "true"),
            exchange = @Exchange(name = "exchange-1",durable = "true",type = "topic"),
            ignoreDeclarationExceptions = "true",key = "springboot.*"))
    public void onMessage(Message message, Channel channel) throws Exception{
        //1.收到消息以后进行业务端消费处理
        System.out.println("--------------------------------");
        System.out.println("消费消息:"+message.getPayload());
        //2.处理成功之后获取deliverTap 并进行收工的ack操作，因为我们配置文件里配置的是手工签收
        Long deliverTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliverTag,false);
    }
}
